import("installer.pkgmanager.xpkgmanager")

function support()
    return {
        windows = true,
        linux = true,
        -- macosx = true
    }
end

function installed()
    return try {
        function()
            os.exec("java -version")
            return true
        end, catch {
            function(e)
                return false
            end
        }
    }
end

function install()
    print("[xlings]: Installing OpenJDK 8...")

    return try {
        function ()
            xpkgmanager.install("openjdk8")            
            return true
        end, catch {
            function (e)
                print("[xlings]: Failed to install OpenJDK 8: " .. e)
                return false
            end
        }
    }
end

function info()
    return {
        name     = "jdk8",
        homepage = "https://adoptium.net",
        author   = "Eclipse Temurin",
        licenses = "GPL v2 with Classpath Exception",
        docs     = "https://docs.oracle.com/javase/8/docs/",
        profile  = "Eclipse Temurin OpenJDK 8",
    }
end


--[[

import("lib.detect.find_tool")
import("platform")
import("common")

local config = platform.get_config_info()

local jdk8_url = {
    linux = "https://github.com/adoptium/temurin8-binaries/releases/download/jdk8u392-b08/OpenJDK8U-jdk_x64_linux_hotspot_8u392b08.tar.gz",
    windows = "https://github.com/adoptium/temurin8-binaries/releases/download/jdk8u392-b08/OpenJDK8U-jdk_x64_windows_hotspot_8u392b08.zip",
    macosx = "https://github.com/adoptium/temurin8-binaries/releases/download/jdk8u392-b08/OpenJDK8U-jdk_x64_mac_hotspot_8u392b08.tar.gz"
}

local jdk8_package = {
    linux = "jdk8.tar.gz",
    windows = "jdk8.zip",
    macosx = "jdk8.tar.gz"
}

local jdk8_file = path.join(config.rcachedir, jdk8_package[os.host()])

function support()
    return {
        windows = true,
        linux = true,
        -- macosx = true
    }
end

function installed()
    return try {
        function()
            os.exec("java -version")
            return true
        end, catch {
            function(e)
                return false
            end
        }
    }
end

function install()
    print("[xlings]: Installing OpenJDK 8...")

    local url = jdk8_url[os.host()]

    if not os.isfile(jdk8_file) then
        common.xlings_download(url, jdk8_file)
    end

    return try {
        function ()
            if os.host() == "windows" then
                os.exec("powershell Expand-Archive -Path " .. jdk8_file .. " -DestinationPath \"C:\\Program Files\\Java\"")
                os.setenv("JAVA_HOME", "C:\\Program Files\\Java\\jdk8u392-b08")
                local path_env = os.getenv("PATH")
                if not string.find(path_env, "JAVA_HOME") then
                    path_env = path_env .. ";" .. os.getenv("JAVA_HOME") .. "\\bin"
                    common.xlings_exec("setx PATH \"" .. path_env .. "\"")
                    os.addenv("PATH", "$JAVA_HOME\\bin")
                end

            elseif os.host() == "linux" then
                os.exec("sudo mkdir -p /usr/lib/jvm")
                os.exec("sudo tar -xzf " .. jdk8_file .. " -C /usr/lib/jvm")
                -- Set environment variables
                os.setenv("JAVA_HOME", "/usr/lib/jvm/jdk8u392-b08")
                os.addenv("PATH", "$JAVA_HOME/bin")
                local bashrc_jdk = 

# auto-generated by xlings
export JAVA_HOME=/usr/lib/jvm/jdk8u392-b08
export PATH=$PATH:$JAVA_HOME/bin


                os.exec("echo '" .. bashrc_jdk .. "' >> ~/.bashrc")

            elseif os.host() == "macosx" then
                -- TODO
            end

            return true
        end, catch {
            function (e)
                os.tryrm(jdk8_file)
                return false
            end
        }
    }
end

function info()
    return {
        name     = "jdk8",
        homepage = "https://adoptium.net",
        author   = "Eclipse Temurin",
        licenses = "GPL v2 with Classpath Exception",
        docs     = "https://docs.oracle.com/javase/8/docs/",
        profile  = "Eclipse Temurin OpenJDK 8",
    }
end

--]]